<?php
namespace notification\tools;
use PHPMailer\PHPMailer\PHPMailer;

class EmailNotification implements NotificationInterface {

    private $mailer;

    public function __construct() {
        $this->mailer = new PHPMailer(true);

        // 配置PHPMailer
        $this->mailer->isSMTP();                                     
        $this->mailer->Host       = 'smtp.qq.com';  // 设置SMTP服务器
        $this->mailer->SMTPAuth   = true;                              
        $this->mailer->Username   = '1296969641@qq.com';  // SMTP 用户名
        $this->mailer->Password   = 'wkqozaklhsrlfhcj';              // SMTP 密码
        $this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;    
        $this->mailer->Port       = 587;                                 
    }

    public function sendText($message, $recipient) {
        $result = true;
        try {
            $this->mailer->setFrom('1296969641@qq.com', '博一文创');
            $this->mailer->addAddress($recipient);     
            
            // 内容
            $this->mailer->isHTML(true);                                  
            $this->mailer->Subject = $message->title;
            $this->mailer->Body    = str_replace("\n", "<br>", $message->content);
            $this->mailer->send();
            // echo 'Message has been sent';
        } catch (\Exception $e) {
            $result = false;
        }
        return $result;
    }
}

